﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.TcCommands.CApAWCHostSessionManager
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using ActiveWorkspaceHosting;
using System;
using System.Collections.Generic;
using System.Linq;
using Tecnomatix.Engineering;


namespace DnProcessSimulateCommands.TcCommands;

public abstract class CApAWCHostSessionManager : ITxAWSUrlFormatProviderEx
{
  private TxAWSConnectionManagerEx m_connectionManager;

  public abstract string DebugEntry { get; }

  public abstract string ComponentId { get; }

  public abstract string GetComponentUrlSuffix(TxAWSConnectionManagerEx connectionManager);

  public CApAWCHostSessionManager()
  {
    this.m_connectionManager = new TxAWSConnectionManagerEx((ITxAWSUrlFormatProviderEx) this);
  }

  protected virtual void RegisterServicesInternal()
  {
  }

  protected virtual void UnregisterServicesInternal()
  {
  }

  internal virtual void OnClientCompleted()
  {
  }

  internal void RegisterServices()
  {
    try
    {
      TxActiveWorkspaceHostManagerEx.Instance.RegisterHostServices();
      this.RegisterServicesInternal();
    }
    catch (Exception ex)
    {
      TxApplication.LogWriter.WriteExceptionLine(ex);
    }
  }

  internal void UnregisterServices()
  {
    TxActiveWorkspaceHostManagerEx.Instance.UnregisterHostServices();
    this.UnregisterServicesInternal();
  }

  internal string GetTargetUri()
  {
    List<KeyValuePair<string, string>> uriAttributes = this.GetUriAttributes();
    return this.AddParametersToUri(this.m_connectionManager.GetTargetUri(), uriAttributes);
  }

  internal bool IsDebugSession => this.m_connectionManager.IsDebugSession;

  protected virtual List<KeyValuePair<string, string>> GetUriAttributes()
  {
    return (List<KeyValuePair<string, string>>) null;
  }

  private string AddParametersToUri(
    string targetUri,
    List<KeyValuePair<string, string>> uriAttributes)
  {
    string uri = targetUri;
    if (!string.IsNullOrEmpty(targetUri) && uriAttributes != null && uriAttributes.Count > 0)
    {
      string attributeFormat = "{0}={1}";
      string str1 = string.Join<string>("&", uriAttributes.Select<KeyValuePair<string, string>, string>((Func<KeyValuePair<string, string>, string>) (x => string.Format(attributeFormat, (object) x.Key, (object) x.Value))));
      if (targetUri.Contains("tcmaMfgSearch"))
        uri = $"{targetUri}&{str1}";
      else if (targetUri.Contains<char>('&'))
      {
        int num = targetUri.IndexOf('&');
        if (num > 0)
        {
          string str2 = str1 + "&";
          uri = targetUri.Insert(num + 1, str2);
        }
      }
    }
    return uri;
  }
}
